home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / MacMud / Unix / align.h next >
Encoding:
C/C++ Source or Header  |  1989-08-15  |  1.7 KB  |  50 lines  |  [TEXT/????]

  1. /* 
  2.  *  'union word' must be the size necessary to ensure that an sbrk()
  3.  *  immediately following an sbrk(sizeof(union word) * n) returns an
  4.  *  address one higher than the first sbrk. i.e. contiguous space from
  5.  *  successive sbrks. This is not vital - the malloc will work, but will
  6.  *  not be able to coalesce between sbrk'ed segments. What is more often
  7.  *  vital is that the things you use the malloc for, like pointers,
  8.  *  structs etc. may need to be aligned on a word/long-word/double-word
  9.  *  boundary depending on the machine. This is the job of the 'foo'
  10.  *  field in the union, which actually decides the size. (On Sun3s, 1
  11.  *  int/long (4 bytes) is good enough, on Sun4s, 8 bytes are necessary,
  12.  *  i.e. 2 ints/longs)
  13.  */
  14. /* Good enough for all machines I use. Change it for your favourite box */
  15. /* need 2 for Sun4s, 1 for Sun3s. */
  16. #if defined(m68k) || defined(mc68000) || defined(vax)
  17. #define ALIGN long foo
  18. #define NALIGN 4
  19. #endif /* m68k || mc68000 || vax */
  20.  
  21. #ifdef sparc
  22. #define ALIGN long foo[2]
  23. #define NALIGN 8
  24. #endif
  25.  
  26. #ifndef ALIGN
  27. You must fix ALIGN before proceeding.
  28. #endif
  29.  
  30. /* Align with power of 2 */
  31. #define    SIMPLEALIGN(X, N) (char *)(((u_long)((char *)(X) + (N-1))) & ~(N-1))
  32.  
  33. #if    NALIGN == 2 || NALIGN == 4 || NALIGN == 8 || NALIGN == 16
  34. /* if NALIGN is a power of 2, the next line will do ok */
  35. #define    ALIGNPTR(X) SIMPLEALIGN(X, NALIGN)
  36. #else
  37. /* otherwise we need the generic version; hope the compiler isn't too smart */
  38. static int nalign = NALIGN;
  39. #define    ALIGNPTR(X) (char *)((((u_long)((char *)(X) + (NALIGN-1)))/nalign)*nalign)
  40. #endif
  41.  
  42. /*
  43.  *  Does sbrk return blocks that are aligned with ALIGN? If not, define
  44.  *  this. I know Suns sbrk() returns aligned stuff. Don't know about
  45.  *  others.
  46.  */
  47. #ifndef sun
  48. #define SBRKUNALIGNED
  49. #endif /* !sun */
  50.